package com.example.demo;

import com.alibaba.fastjson.JSON;
import com.example.demo.bean.Person;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;

/**
 * list转map
 */
@Slf4j
public class ListToMap {

  public static void main(String[] args) {
    Person person1 = new Person();
    person1.setName("徐瑞强");
    person1.setCreateTime(new Date());
    person1.setAge(10);
    person1.setPassword("111");

    Person person2 = new Person();
    person2.setName("徐瑞琪");
    person2.setCreateTime(new Date());
    person2.setAge(20);
    person2.setPassword("222");

    List<Person> people = new ArrayList<>();
    people.add(person1);
    people.add(person2);
    log.info("LIST：{}", JSON.toJSONString(people));

    log.info("MAP：{}", JSON.toJSONString(getIdNameMap(people)));

  }

  public static Map<Integer, String> getIdNameMap(List<Person> accounts) {
    return accounts.stream().collect(Collectors.toMap(Person::getAge, Person::getName));
  }
}
